﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace NPMonitor.Utility
{
    public class Requests
    {
        public int RequestId { get; set; }
        public string Url { get; set; }
        public int ProcessId { get; set; }
        public string Location { get; set; }
        public string MachineName { get; set; }
        public int GatewayDeterminationTime { get; set; }
        public int DNSTime { get; set; }
        public int TCPConnectTime { get; set; }
        public int HTTPSHandshake { get; set; }
        public int ElapsedTime { get; set; }
        public DateTime RequestTime { get; set; }
        public int ClientTime { get; set; }
        public int NetworkTime { get; set; }
        public int ServerTime { get; set; }
        public DateTime ClientConnected { get; set; }
        public DateTime ClientBeginRequest;
        public DateTime GotRequestHeaders;
        public DateTime ClientDoneRequest;
        public Nullable<DateTime> ServerConnected;
        public Nullable<DateTime> ServerGotRequest;
        public Nullable<DateTime> ServerBeginResponse;
        public Nullable<DateTime> GotResponseHeaders;
        public Nullable<DateTime> ServerDoneResponse;
        public DateTime ClientBeginResponse;
        public DateTime ClientDoneResponse;


        public DateTime RequestTimeFormat
        {
            get
            {
                return new DateTime(RequestTime.Year, RequestTime.Month, RequestTime.Day
                     , RequestTime.Hour, (RequestTime.Minute / 5) * 5, 0);
            }
        }

        protected string InsertSql
        {
            get
            {
                return string.Format(@"INSERT INTO [Requests]
                            (ProcessId,[URL] ,[Location],[MachineName] ,[GatewayDeterminationTime] ,[DNSTime],[TCPConnectTime],[HTTPSHandshake]
                                ,[ElapsedTime] ,[RequestTime],[RequestTimeFormat],ClientTime,NetworkTime,ServerTime
                                ,ClientConnected
                                ,ClientBeginRequest
                                ,GotRequestHeaders
                                ,ClientDoneRequest
                                ,ServerConnected
                                ,ServerGotRequest
                                ,ServerBeginResponse
                                ,GotResponseHeaders
                                ,ServerDoneResponse
                                ,ClientBeginResponse
                                ,ClientDoneResponse)
                            VALUES(@ProcessId,@Url,@Location,@MachineName,@GatewayDeterminationTime,@DNSTime,@TCPConnectTime,@HTTPSHandshake
                                ,@ElapsedTime,@RequestTime,@RequestTimeFormat,@ClientTime,@NetworkTime,@ServerTime
                                ,@ClientConnected
                                ,@ClientBeginRequest
                                ,@GotRequestHeaders
                                ,@ClientDoneRequest
                                ,@ServerConnected
                                ,@ServerGotRequest
                                ,@ServerBeginResponse
                                ,@GotResponseHeaders
                                ,@ServerDoneResponse
                                ,@ClientBeginResponse
                                ,@ClientDoneResponse)");
            }
        }

        public void Insert()
        {
            //ILog log = LogFactory.CreateFileLog();
            //log.Info(InsertSql);
            //Utility.SqlHelper.ExecuteNonQuery(InsertSql);
            SqlParameter[] paras = {
                                    new SqlParameter("@ProcessId",ProcessId),
                                    new SqlParameter("@Url", Url),
                                    new SqlParameter("@Location", Location),
                                    new SqlParameter("@MachineName",MachineName),
                                    new SqlParameter("@GatewayDeterminationTime", GatewayDeterminationTime),
                                    new SqlParameter("@DNSTime", DNSTime),
                                    new SqlParameter("@TCPConnectTime", TCPConnectTime),
                                    new SqlParameter("@HTTPSHandshake",HTTPSHandshake),
                                    new SqlParameter("@ElapsedTime", ElapsedTime),
                                    new SqlParameter("@RequestTime", RequestTime),
                                    new SqlParameter("@RequestTimeFormat", RequestTimeFormat),
                                    new SqlParameter("@ClientTime",ClientTime),
                                    new SqlParameter("@ServerTime",ServerTime),
                                    new SqlParameter("@NetworkTime",NetworkTime),
                                    new SqlParameter("@ClientConnected",ClientConnected),
                                    new SqlParameter("@ClientBeginRequest",ClientBeginRequest),
                                    new SqlParameter("@GotRequestHeaders",GotRequestHeaders),
                                    new SqlParameter("@ClientDoneRequest",ClientDoneRequest),
                                    new SqlParameter("@ServerConnected",ServerConnected),
                                    new SqlParameter("@ServerGotRequest",ServerGotRequest),
                                    new SqlParameter("@ServerBeginResponse",ServerBeginResponse),
                                    new SqlParameter("@GotResponseHeaders",GotResponseHeaders),
                                    new SqlParameter("@ServerDoneResponse",ServerDoneResponse),
                                    new SqlParameter("@ClientBeginResponse",ClientBeginResponse),
                                    new SqlParameter("@ClientDoneResponse",ClientDoneResponse),
                                   };
            SqlHelper.ExecuteNonQuery(InsertSql, paras);
        }
    }
}
